package com.ai3d.rfid.common.utils;

import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 说明：日期格式化工具
 * 作者：liuwan1992
 * 添加时间：2018/12/17
 * 修改人：liuwan1992
 * 修改时间：2018/12/18
 */
public class DateFormatUtils {

    public static final String DATE_FORMAT_PATTERN_YMD_HM = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DATE_FORMAT_PATTERN_YMD = "yyyy-MM-dd";
    public static final String DATE_FORMAT_PATTERN_YMD2 = "yyyy年MM月dd日";
    public static final String DATE_FORMAT_PATTERN_YMD3 = "yyyy/MM/dd";
    public static final String DATE_FORMAT_PATTERN_YMD4 = "yyyy年MM月dd日 HH:mm";
    public static final String DATE_FORMAT_PATTERN_YMD5 = "HH:mm";
    public static final String DATE_FORMAT_PATTERN_YMD6 = "MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_PATTERN_YMD7 = "yyyy年MM月dd日 HH";
    public static final String DATE_FORMAT_PATTERN_HM = "HH:mm:ss";

    /**
     * 时间戳转字符串
     *
     * @param timestamp 时间戳
     * @return 格式化的日期字符串
     */
    public static String long2Str(long timestamp, String pattern) {
        try {
            return new SimpleDateFormat(pattern, Locale.CHINA).format(new Date(timestamp));
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 时间戳转字符串
     *
     * @param timestamp 时间戳
     * @return 格式化的日期字符串
     */
    public static String long2Str(String timestamp, String pattern) {
        if (TextUtils.isEmpty(timestamp)) {
            return "";
        }
        long tempValue = 0;
        try {
            tempValue = Long.parseLong(timestamp);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return new SimpleDateFormat(pattern, Locale.CHINA).format(new Date(tempValue));
    }

    /**
     * 字符串转时间戳
     *
     * @param dateStr 日期字符串
     * @return 时间戳
     */
    public static long str2Long(String dateStr, String pattern) {
        try {
            return new SimpleDateFormat(pattern, Locale.CHINA).parse(dateStr).getTime();
        } catch (Throwable ignored) {
        }
        return 0;
    }

    /**
     * 字符串转时间戳
     *
     * @param date 日期字符串
     * @return 时间戳
     */
    public static String date2Str(Date date, String pattern) {
        try {
            return new SimpleDateFormat(pattern, Locale.CHINA).format(date);
        } catch (Throwable ignored) {
        }
        return "";
    }
}
